
clear all
cap log close


// The codes comment out here are used for generate the intermediate input data. Users should run them the first time and then leave them comment out. 


/*
use "${dataout}allmatches_allyears_matchpast5", clear
keep  sykstun year_event defendant_switch
duplicates drop 
collapse (max) defendant_switch, by(sykstun year_event)
save "${dataout}defendant_switch_firm_level", replace

use "${dataout}allmatches_allyears_matchpast5", clear
keep  sykstun year_event time employed
keep if time >=0 & time <=5
duplicates drop 
collapse (sum) employed, by(sykstun year_event)
gen unemployed = 6 - employed
save "${dataout}defendant_ue_firm_level", replace


use "${dataout}allmatches_allyears_matchpast5", clear
drop if wp_crime==0
g tmp = 0
replace tmp = tyotu if time==-1
bys shnro year_event : egen pre_inc = max(tmp)
drop tmp

g tmp = 0
replace tmp = ika if time==-1
bys shnro year_event : egen pre_age = max(tmp)
drop tmp

g crimes = 0
replace crimes=1 if crimecode6=="210501"
replace crimes=2 if crimecode6=="210701"
replace crimes=3 if crimecode6=="211001"
replace crimes=4 if crimecode6=="250701"

collapse (mean) pre_inc pre_age crimes, by(sykstun year_event)
replace crimes = round(crimes)
save "${dataout}defendant_crime_char_firm_level", replace
*/

//create firm char for_topwomen
/* 

use "W:\workplace\data\police_DV_workplace_victim_2000.dta", clear
foreach Y of numlist 2001/2018{
    append using "W:\workplace\data\police_DV_workplace_victim_`Y'.dta"
} 


rename victim_sykstun_lag sykstun_lag
rename victim_sykstun sykstun
// summary statistics for firm characteristics by crime types 
gen mm=(suspect_sex=="1" & plaintiff_sex=="1") 
gen mf=(suspect_sex=="1" & plaintiff_sex=="2")
replace sykstun=sykstun_lag if sykstun== ""

collapse (max) mm (max) mf wp_crime, by(sykstun year_event)

* one year before the event 
gen year=year_event-1 
merge m:1 sykstun year using "${dataout}plant_char_allyears"
keep if _merge==3
drop _merge 

replace wp_crime=0 if wp_crime==. 
drop if year<1999


drop if employee==1
* topcoded firm size 
replace employee=5000 if employee>5000
replace female_employee=3000 if female_employee>3000

gen share_newhire=newhire/employee
gen share_female_newhire=female_newhire/employee
gen share_switch=-switch/employee
gen share_switch_female=-switch_female/employee

gen pay_gap=male_earnings-female_earnings

gen share_female_manager=female_manager/(male_manager+female_manager)

g female_firm = (share_female_top > .04)
su share_female_top if wp_crime ==1 & mf==1, detail
g female_industry = (share_ftop_ind > .16)


foreach thing in employee earnings age tenure share_female {
    su `thing', detail
	local med = r(p50)
	g med_`thing' = (`thing' >= `med')
}
/*
reghdfe female_firm med_* college high_school , absorb(nace year_event) cluster(sykstun) 
est sto femalefirm_all_reg
reghdfe female_firm med_* college high_school if mf==1, absorb(nace year_event) cluster(sykstun) 
est sto femalefirm_mf_reg
reghdfe female_firm med_* college high_school if mm==1, absorb(nace year_event) cluster(sykstun) 
est sto femalefirm_mm_reg

esttab femalefirm_all_reg femalefirm_mf_reg femalefirm_mm_reg using "$results/table_femalefirm_char.tex", ///
		noconstant b(4) se(4) replace
*/
ren college college_firm
ren high_school high_school_firm
keep sykstun year_event med_* college_firm high_school_firm
duplicates drop 
save "$dataout/firm_char_for_topwomen.dta", replace 


*/ 

*Graph style
sysdir set PERSONAL "W:\ado"
set scheme kailaScheme


global dummies =  "dpl_5 dpl_4 dpl_3 dpl_2 dpl_0 dpl1 dpl2 dpl3 dpl4 dpl5"
global fe_full = "baseid time year" 
global cluster = "sykstun"


	
	* merge defendants data with new hires 
	use "${dataout}turnover_allyears_violentfirms", clear
	
	merge m:1 sykstun year_event using "${dataout}defendant_switch_firm_level"
	drop if _merge==2 
	drop _merge 
	
	merge m:1 sykstun year_event using "${dataout}defendant_ue_firm_level"
	drop if _merge==2 
	drop _merge 
	
 	merge m:1 sykstun year_event using "${dataout}defendant_crime_char_firm_level"
	drop if _merge==2 
	drop _merge
	replace pre_age = 0 if missing(pre_age)
	replace pre_inc = 0 if missing(pre_inc)
	replace crimes=0 if missing(crimes)
	
	rename share_female share_female3
	gen share_female1=female_employee/baseline_employee
	replace nace=floor(nace)
	

	
	* share women in top jobs in a firm heterogeneity
	
	su share_female_top, detail
	local med = r(p50)

	g female_firm = (share_female_top > `med')
	
	su share_female_top if time==0 & wp_crime ==1 & mf==1, detail
	
	g share_female_nottop = (share_female3 - 0.2*share_female_top)/0.8
	replace share_female_nottop = 0 if share_female_nottop <0
	replace share_female_nottop = 1 if share_female_nottop >1
	
	
	
	* share women in top jobs in an industry 
	
	su share_ftop_ind, detail
	local med_ind = r(p50)

	g female_industry = (share_ftop_ind > `med_ind')

	 sort match_id1 time sykstun
     bys match_id1 time: ereplace mm=max(mm)
     bys match_id1 time: ereplace mf=max(mf)
	 bys match_id1 year_event time: ereplace new_wp_crime=max(new_wp_crime)
     keep if new_wp_crime==1 
	 


* newhire and turnover def 
sort sykstun year_event time
bys sykstun year_event: gen share_switch3=-switch/employee
bys sykstun year_event: gen share_fswitch3=-switch_female/employee
bys sykstun year_event: gen share_mswitch3=-switch_male/employee


sum share_fswitch3,d
local top1= r(p99)
replace  share_fswitch3=`top1' if share_fswitch3>`top1'

bys sykstun year_event: gen share_newhire3=newhire/employee
bys sykstun year_event: gen share_fnewhire3=female_newhire/employee
bys sykstun year_event: gen share_mnewhire3=male_newhire/employee
	 
	 
	 
	 
	 bys match_id1 year_event time: ereplace manager_sub=max(manager_sub)
     bys match_id1 year_event time: ereplace manager_sub2=max(manager_sub2)

	  
     replace manager_sub=0 if manager_sub==. 
	 replace manager_sub2=0 if manager_sub2==.
	 sort sykstun year_event
	merge m:1 sykstun year_event using "$dataout/firm_char_for_topwomen.dta"
	drop if _merge==2
	drop _merge
	foreach thing in employee earnings age tenure share_female  {
	    replace med_`thing' = 0 if missing(med_`thing')
	}
	replace college_firm = 0 if missing(college_firm)
	replace high_school_firm=0 if missing(high_school_firm)
	

	gen baseyr=year_event

	assert time >= -5 & time <= 5 

	gen treat= wp_crime==1


	replace defendant_switch=0 if defendant_switch==. 


	gen treatPost= treat==1 & time>0
	gen iteraction_occ=treatPost*manager_sub
	gen iteraction_con_topfirm=treatPost*share_female_top
	gen iteraction_con_topind=treatPost*share_ftop_ind
	gen iteraction_con_perpleave=treatPost*share_female_top*defendant_switch
	gen iteraction_con_control=treatPost*defendant_switch
	
	gen iteraction_ue = treatPost*(unemployed >= 1)
	gen iteraction_ue_fem = treatPost*(unemployed >= 1)*female_firm
	
	gen iteraction_dummy_topfirm=treatPost*female_firm
	gen iteraction_dummy_topind=treatPost*female_industry
	gen iteraction_dummy_perpleave=treatPost*female_firm*defendant_switch
	gen iteraction_dummy_control=treatPost*defendant_switch
	
	foreach thing in employee earnings age tenure share_female  {
	    g inter_`thing' = treatPost*med_`thing'
	}
	
	g tmp = 0
	replace tmp = share_female3 if time == -1
	bys sykstun year_event : egen base_fem = max(tmp)
	drop tmp 
	
	su base_fem, detail
	local med = r(p50)

	g female_firm_all = (share_female3 > `med')
	g iteraction_fem_all = treatPost*female_firm_all
	

	* Generate treatment wave indentifier for each individual
	egen baseid=group(sykstun)
	
	reghdfe share_female3 treatPost if mf==1, absorb($fe_full)  cluster($cluster)
			 
	reghdfe share_female3 treatPost  if mm==1, absorb($fe_full)  cluster($cluster)
	

	
	// MF
	reghdfe share_female3 treatPost iteraction_dummy_topfirm iteraction_dummy_topind ///
			 female_firm female_industr if mf==1, absorb($fe_full)  cluster($cluster)
	est sto reg_1		 

	reghdfe share_female3 treatPost iteraction_ue if mf==1, absorb($fe_full)  cluster($cluster)
	est sto reg_2
	
	reghdfe share_female3 iteraction_ue iteraction_dummy_topfirm  iteraction_ue_fem treatPost iteraction_dummy_topind  ///
			 female_firm female_industr if mf==1, absorb($fe_full)  cluster($cluster)
	est sto reg_3
	
	reghdfe share_female3 treatPost iteraction_dummy_topfirm iteraction_dummy_topind ///
			 female_firm female_industr inter_* if mf==1, absorb($fe_full)  cluster($cluster)
	est sto reg_4		 

	reghdfe share_female3 treatPost iteraction_ue inter_* if mf==1, absorb($fe_full)  cluster($cluster)
	est sto reg_5
	
	reghdfe share_female3 iteraction_ue iteraction_dummy_topfirm  iteraction_ue_fem treatPost iteraction_dummy_topind  ///
			 female_firm female_industr inter_* if mf==1, absorb($fe_full)  cluster($cluster)
	est sto reg_6
	
		
	esttab reg_1  reg_2 reg_3 reg_4 reg_5 reg_6 using "${results}fem_manage_ue_interaction_robust.tex", replace b(4) se(4) r2(4) ///
			keep(treatPost iteraction_dummy_topfirm iteraction_ue*)